package com.ukao.pad.printer;

import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.util.Log;

import com.ukao.pad.R;
import com.ukao.pad.bean.PrinterBean;
import com.ukao.pad.utils.L;
import com.ukao.pad.utils.T;
import com.ukao.pad.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by ukaodev on 2017/12/23.
 * 检测打印助手
 */

public class CheckPrinterHelper {




    /**
     * 是否是isWashprinter打印机 打标
     *   水洗唛打印机
     */
    public static   boolean   isWashprinter(   ){
        boolean  isWashprinter=false;
        UsbManager manager = (UsbManager) Utils.getContext().getSystemService(Context.USB_SERVICE);
        Iterator<UsbDevice> deviceIterator = manager.getDeviceList().values().iterator();
        while(deviceIterator.hasNext()) {
            UsbDevice usbDevices = deviceIterator.next();
            if (usbDevices.getDeviceClass() == 0) {
                UsbInterface anInterface = usbDevices.getInterface(0);
                if (anInterface.getInterfaceClass() == 7) {
                    if(usbDevices.getVendorId() == 4611 && usbDevices.getProductId() == 294){/*水洗唛打印机*/
                        isWashprinter=true;

                    }else  if (usbDevices.getVendorId() == 4611 && usbDevices.getProductId() == 310){
                        isWashprinter=true;
                    }

                }

            }



        }
        if (!isWashprinter){
            T.show("未连接水洗唛打印机");
        }
        return  isWashprinter;
    }


    /**
     * 是否是isWashprinter  T210E 打印机
     *
     */
    public    static boolean   isT210EWashprinter(   ){
        boolean  isWashprinter=false;
        UsbManager manager = (UsbManager) Utils.getContext().getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
        StringBuilder sb = new StringBuilder();
        while (deviceIterator.hasNext()) {
            UsbDevice usbDevices = deviceIterator.next();
            int deviceClass = usbDevices.getDeviceClass();
            if (deviceClass == 0) {
                UsbInterface anInterface = usbDevices.getInterface(0);

                if (anInterface.getInterfaceClass() == 7) {
                    if(usbDevices.getVendorId() == 4611 && usbDevices.getProductId() == 310){/*水洗唛打印机*/
                        isWashprinter=true;

                    }


                } else if (anInterface.getInterfaceClass() == 8) {
                    sb.append("此设备是U盘\n");
                }

            }



        }


        return  isWashprinter;
    }



    /**
     * 是否是标签打印机
     *
     */
    public  static  boolean   isTagPrinter(   ){
        boolean  isWashprinter=false;
        UsbManager manager = (UsbManager) Utils.getContext().getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
        StringBuilder sb = new StringBuilder();
        while (deviceIterator.hasNext()) {
            UsbDevice usbDevices = deviceIterator.next();
            int deviceClass = usbDevices.getDeviceClass();
            if (deviceClass == 0) {
                UsbInterface anInterface = usbDevices.getInterface(0);
                int interfaceClass = anInterface.getInterfaceClass();
                if (anInterface.getInterfaceClass() == 7) {
                    if(usbDevices.getVendorId() == 4611 && usbDevices.getProductId() == 370){/*tsc打印机*/
                        isWashprinter=true;

                    }


                } else if (anInterface.getInterfaceClass() == 8) {
                    sb.append("此设备是U盘\n");
                }

            }



        }

        if (!isWashprinter){
            T.show("未连接标签打印机");
        }
        return  isWashprinter;
    }


    /**
     * 获取打印设备
     * @return
     */
    public  static List<PrinterBean> loadPrinterList(  ) {

        UsbManager manager = (UsbManager) Utils.getContext().getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
        StringBuilder sb = new StringBuilder();
        List<PrinterBean>  mData=new ArrayList<>();
        while (deviceIterator.hasNext()) {
            UsbDevice usbDevice = deviceIterator.next();
            sb.append("DeviceName=" + usbDevice.getDeviceName() + "\n");
            sb.append("DeviceId=" + usbDevice.getDeviceId() + "\n");
            sb.append("VendorId=" + usbDevice.getVendorId() + "\n");
            sb.append("ProductId=" + usbDevice.getProductId() + "\n");
            sb.append("DeviceClass=" + usbDevice.getDeviceClass() + "\n");
            int deviceClass = usbDevice.getDeviceClass();
            if (deviceClass == 0) {
                UsbInterface anInterface = usbDevice.getInterface(0);
                int interfaceClass = anInterface.getInterfaceClass();

                sb.append("device Class 为0-------------\n");
                sb.append("Interface.describeContents()=" + anInterface.describeContents() + "\n");
                sb.append("Interface.getEndpointCount()=" + anInterface.getEndpointCount() + "\n");
                sb.append("Interface.getId()=" + anInterface.getId() + "\n");
                //http://blog.csdn.net/u013686019/article/details/50409421
                //http://www.usb.org/developers/defined_class/#BaseClassFFh
                //通过下面的InterfaceClass 来判断到底是哪一种的，例如7就是打印机，8就是usb的U盘
                sb.append("Interface.getInterfaceClass()=" + anInterface.getInterfaceClass() + "\n");
                if (anInterface.getInterfaceClass() == 7) {
                    sb.append("此设备是打印机\n");
                    if(usbDevice.getVendorId() == 4611 && usbDevice.getProductId() == 370){/*标签*/
                        String	washPrint=Utils.getContext().getResources().getString(R.string.receipt_print);
                        String	washcode=Utils.getContext().getResources().getString(R.string.receipt_code);
                        mData.add(new PrinterBean(washPrint,washcode,false));

                    }else if(usbDevice.getVendorId() == 34918 && usbDevice.getProductId() == 256) {/*小票打印机*/
                        String	washPrint=Utils.getContext().getResources().getString(R.string.detailed_print);
                        String	washcode=Utils.getContext().getResources().getString(R.string.detailed_code);

                        mData.add(new PrinterBean(washPrint,washcode,true));
                    }else if(usbDevice.getVendorId() == 4611 && usbDevice.getProductId() == 294){/*水洗唛打印机*/
                        String	washPrint=Utils.getContext().getResources().getString(R.string.wash_print);
                        String	washcode=Utils.getContext().getResources().getString(R.string.wash_code);
                        mData.add(new PrinterBean(washPrint,washcode,true));
                    }else if(usbDevice.getVendorId() == 4611 && usbDevice.getProductId() == 310) {/*TSC T-210E清单打印*/

                        String	washcode=Utils.getContext().getResources().getString(R.string.detailed);
                        String	washPrint=Utils.getContext().getResources().getString(R.string.receipt_print);
                        mData.add(new PrinterBean(washPrint,washcode,false));
                    }



                } else if (anInterface.getInterfaceClass() == 8) {
                    sb.append("此设备是U盘\n");
                }
                sb.append("anInterface.getInterfaceProtocol()=" + anInterface.getInterfaceProtocol() + "\n");
                sb.append("anInterface.getInterfaceSubclass()=" + anInterface.getInterfaceSubclass() + "\n");
                sb.append("device Class 为0------end-------\n");
            }

            sb.append("DeviceProtocol=" + usbDevice.getDeviceProtocol() + "\n");
            sb.append("DeviceSubclass=" + usbDevice.getDeviceSubclass() + "\n");

            sb.append("                           \n");
        }
        L.i("PrinterBean="+sb.toString());

        return   mData;
    }

    /**
     * 是否是小票
     * @return
     */
    public  static   boolean   isReceipt(   ){
        boolean  printertype=false;
        UsbManager manager = (UsbManager) Utils.getContext().getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
        StringBuilder sb = new StringBuilder();
        while (deviceIterator.hasNext()) {
            UsbDevice usbDevices = deviceIterator.next();
            int deviceClass = usbDevices.getDeviceClass();
            if (deviceClass == 0) {
                UsbInterface anInterface = usbDevices.getInterface(0);

                if (anInterface.getInterfaceClass() == 7) {
                    sb.append("此设备是打印机\n");
                    if (usbDevices.getVendorId() == 34918 && usbDevices.getProductId() == 256){//"GP-L80160I打印机已成功连接
                        printertype=true;

                    }


                } else if (anInterface.getInterfaceClass() == 8) {
                    sb.append("此设备是U盘\n");
                }

            }



        }

        if (!printertype){
            T.show("未连接小票打印机");
        }
        return  printertype;
    }

    /**
     * 是否是连接读卡器
     *
     */
    public static UsbDevice    getConnectCardReader(){

        UsbManager mUsbManager = (UsbManager) Utils.getContext().getSystemService(Context.USB_SERVICE);
        if(mUsbManager!= null) {
            HashMap<String, UsbDevice> deviceHashMap = mUsbManager.getDeviceList();
            Iterator<UsbDevice> iterator = deviceHashMap.values().iterator();
            //循环找读卡器设备
            while (iterator.hasNext()) {
                UsbDevice   device= iterator.next();
                int mVendorId = device.getVendorId();
                int mmProductId = device.getProductId();
                L.i( "vendor id:" +mVendorId+ "  mProductId: " +mmProductId );
                if (mVendorId==65535 && mmProductId == 53) {

                    return device;
                }
            }
        }else {
            Log.i("Debug:","-------------usbManager is null");
        }
        return null;
    }

}
